﻿using System;
using System.Data;
using System.Net.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDDLGroup();
        }
    }

    public void BindDDLGroup()
    {
        DataSet dst = EventDAO.BindDDLGroup();
        ddlGroup.DataSource = dst.Tables[0];
        ddlGroup.DataTextField = "UserType";
        ddlGroup.DataValueField = "UserTypeId";
        ddlGroup.DataBind();
    }

    protected void btnCreateUser_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            try
            {
                User user = new User();
                user.userTypeId = Convert.ToInt32(ddlGroup.SelectedItem.Value);
                user.userName = UserName.Text.ToLower();
                user.loginName = LoginName.Text.ToLower();
                user.password = Password.Text;
                user.mobileNumber = MobileNumber.Text;
                user.emailId = Email.Text;
                user.createdDate = DateTime.Now;
                user.modifiedDate = DateTime.Now;

                bool isUserPresent = EventDAO.isUserPresent(user);

                if (!isUserPresent)
                {
                    int userId = EventDAO.addUser(user);

                    if (userId != 0)
                    {
                        User u = EventDAO.getUserById(userId);
                        Session["LoginName"] = u.loginName;
                        Session["UserType"] = u.userType;
                        Session["UserId"] = u.userId;
                        Session["UserTypeId"] = u.userTypeId;
                        Session["MobileNumber"] = u.mobileNumber;
                        Session["EmailId"] = u.emailId;

                        // Send an email logic here. to the specified user and administrator as well.
                        SendEmail(u);
                        lblStatus.Text = "You have been succesfully registered. Please click here to login.";
                        
                        lnkBtnLogin.Visible = true;
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key_name", "alert('Please try to register later.');", true);
                    }
                }
                else
                {
                    // You have already registered in the system.
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key_name", "alert('You have already registered into our system.');", true);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }


    private static void SendEmail(User u)
    {
        MailMessage mailMessage = new MailMessage();
        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com",587);
        smtpClient.EnableSsl = true;
        smtpClient.Credentials = new System.Net.NetworkCredential("organizerwork@gmail.com", "Rizwan@123"); // User id and password.
        mailMessage.From = new MailAddress("organizerwork@gmail.com");
        mailMessage.To.Add(new MailAddress("organizerwork@gmail.com"));
        mailMessage.To.Add(new MailAddress(u.emailId));
        mailMessage.ReplyTo = new MailAddress("organizerwork@gmail.com");
        mailMessage.Subject = "Welcome to Work Organizer";
        mailMessage.Body = "Dear " + u.loginName + " \n\nAs per your request, your account has been created and your credentials are \n\n login name: " + u.userName + "\n password : " + u.password + "\n\nRegards \n admin.";
        smtpClient.Send(mailMessage);
    }


}
